// pages/oa/mine/index.js
var orgid = "";
var userId = "";

var api = require("../../../utils/api.js");
var util = require('../../../utils/util.js');
var isExamine = "2";//审批状态2:审批中  0或1 已完成审批
var sendUserId = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: ['审批中', '已完成', '抄送我的'],
    currentTab: 0,
    statuslist: [
      // { id: "0", text: "业务审批", amount: "0", show: "Y" },
      // { id: "1", text: "用印审批", amount: "0", show: "Y" },
      { id: "2", text: "财务审批", amount: "0", show: "Y" },
      // { id: "3", text: "人事审批", amount: "0", show: "Y" },
      // { id: "4", text: "行政审批", amount: "0", show: "Y" },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "我的申请"
    });
    var that = this;
    that.myOaApplyCount("0");
  },

  //审批中 已完成
  myOaApplyCount: function (type) {
    var that = this;
    userId = wx.getStorageSync('userId');
    orgid = wx.getStorageSync('companyId');
    wx.showLoading({
    });
    api.fetchApi2("myOaApply/myOaApplyCount?orgid=" + orgid + "&userId=" + userId, {
    }, "POST").then(res => {
      wx.hideLoading();
      // if (res.data.oaApplyCount == null) {

      //   that.setData({
      //     // 'statuslist[0].amount': 0,
      //     // 'statuslist[1].amount': 0,
      //     'statuslist[0].amount': 0,
      //     // 'statuslist[3].amount': 0,
      //     // 'statuslist[4].amount': 0
      //   })
      //   return false;
      // }
      if (type == "1") {
        //已完成的申请
        that.setData({
          // 'statuslist[0].amount': res.data.oaApplyCount.okExamineCaseNum,
          // 'statuslist[1].amount': res.data.oaApplyCount.okEndCaseNum,
          'statuslist[0].amount': res.data.okBusinessNum,
          // 'statuslist[3].amount': res.data.oaApplyCount.okPrintNum,
          // 'statuslist[4].amount': res.data.oaApplyCount.okInvoiceNum
        })
      } else {
        that.setData({
          // 'statuslist[0].amount': res.data.oaApplyCount.doExamineCaseNum,
          // 'statuslist[1].amount': res.data.oaApplyCount.doEndCaseNum,
          'statuslist[0].amount': res.data.doBusinessNum,
          // 'statuslist[3].amount': res.data.oaApplyCount.doPrintNum,
          // 'statuslist[4].amount': res.data.oaApplyCount.doInvoiceNum
        })

      }
    });

  },
  //抄送我的
  myCopyOaApplyCount: function () {
    var that = this;
    wx.showLoading({
    });
    api.fetchApi2("myOaApply/myCopyOaApplyCount?orgid=" + orgid + "&userId=" + userId, {
    }, "POST").then(res => {
      wx.hideLoading();
      // if (res.data.oaApplyCount == null) {
      //   that.setData({
      //     // 'statuslist[0].amount': 0,
      //     // 'statuslist[1].amount': 0,
      //     'statuslist[0].amount': 0,
      //     // 'statuslist[3].amount': 0,
      //     // 'statuslist[4].amount': 0
      //   })
      //   return false;
      // }
      that.setData({
        // 'statuslist[0].amount': res.data.oaApplyCount.doExamineCaseNum,
        // 'statuslist[1].amount': res.data.oaApplyCount.doEndCaseNum,
        'statuslist[0].amount': res.data.doBusinessNum,
        // 'statuslist[3].amount': res.data.oaApplyCount.doPrintNum,
        // 'statuslist[4].amount': res.data.oaApplyCount.doInvoiceNum
      })
    });

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //响应点击导航栏
  navbarTap: function (e) {
    var that = this;
    var idx = e.currentTarget.dataset.idx;
    that.setData({
      currentTab: e.currentTarget.dataset.idx,
      TypeItem: that.data.navbar[that.data.currentTab]
    });
    if (idx == 0) {
      isExamine = "2";
      sendUserId = "";
      that.myOaApplyCount(idx);
    } else if (idx == 1) {
      that.myOaApplyCount(idx);
      sendUserId = "";
      isExamine = "0,1";
    }
    else if (idx == 2) {
      isExamine = "";
      sendUserId = userId;
      that.myCopyOaApplyCount(idx);
    }
  },
  redirectTo: function (e) {
    var that = this;
    var idx = e.currentTarget.id;
    wx.navigateTo({
      url: '../approval/myoalist?id=' + idx + "&isExamine=" + isExamine + "&sendUserId=" + sendUserId
    });
  },
})